<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Choosing an API</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqlinfo.terminology.html">Terminology overview</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqlinfo.library.choosing.html">Choosing a library</a></div>
 <div class="up"><a href="mysql.html">Overview of the MySQL PHP drivers</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mysqlinfo.api.choosing" class="chapter">
   <h1>Choosing an API</h1>

   <p class="para">
    PHP offers three different APIs to connect to MySQL. Below we show
    the APIs provided by the mysql, mysqli, and PDO extensions. Each code snippet
    creates a connection to a MySQL server running on &quot;example.com&quot; using
    the username &quot;user&quot; and the password &quot;password&quot;. And a query is run to
    greet the user.
   </p>
   <p class="para">
    <div class="example" id="example-1523">
     <p><strong>Example #1 Comparing the three MySQL APIs</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;mysqli<br /></span><span style="color: #0000BB">$mysqli&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"example.com"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"database"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;'Hello,&nbsp;dear&nbsp;MySQL&nbsp;user!'&nbsp;AS&nbsp;_message&nbsp;FROM&nbsp;DUAL"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_assoc</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'_message'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">//&nbsp;PDO<br /></span><span style="color: #0000BB">$pdo&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">'mysql:host=example.com;dbname=database'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'user'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'password'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$statement&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$pdo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;'Hello,&nbsp;dear&nbsp;MySQL&nbsp;user!'&nbsp;AS&nbsp;_message&nbsp;FROM&nbsp;DUAL"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$statement</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_ASSOC</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'_message'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">//&nbsp;mysql<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"example.com"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">"database"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;'Hello,&nbsp;dear&nbsp;MySQL&nbsp;user!'&nbsp;AS&nbsp;_message&nbsp;FROM&nbsp;DUAL"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_fetch_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'_message'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="para">
    <em class="emphasis">Recommended API</em>
   </p>
   <p class="para">
    It is recommended to use either the <a href="book.mysqli.html" class="link">mysqli</a>
    or <a href="ref.pdo-mysql.html" class="link">PDO_MySQL</a> extensions.
    It is not recommended to use the old <a href="ref.mysql.html" class="link">mysql</a>
    extension for new development, as it has been deprecated as of PHP 5.5.0
    and will be removed in the future. A detailed feature comparison matrix is
    provided below. The overall performance of all three extensions is
    considered to be about the same. Although the performance of the extension
    contributes only a fraction of the total run time of a PHP web request.
    Often, the impact is as low as 0.1%.
   </p>
   <p class="para">
    <em class="emphasis">Feature comparison</em>
   </p>
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th class="empty">&nbsp;</th>
       <th>ext/mysqli</th>
       <th>PDO_MySQL</th>
       <th>ext/mysql</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>PHP version introduced</td>
       <td>5.0</td>
       <td>5.1</td>
       <td>2.0</td>
      </tr>

      <tr>
       <td>Included with PHP 5.x</td>
       <td>Yes</td>
       <td>Yes</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>Development status</td>
       <td>Active</td>
       <td>Active</td>
       <td>Maintenance only</td>
      </tr>

      <tr>
       <td>Lifecycle</td>
       <td>Active</td>
       <td>Active</td>
       <td>Deprecated</td>
      </tr>

      <tr>
       <td>Recommended for new projects</td>
       <td>Yes</td>
       <td>Yes</td>
       <td>No</td>
      </tr>

      <tr>
       <td>OOP Interface</td>
       <td>Yes</td>
       <td>Yes</td>
       <td>No</td>
      </tr>

      <tr>
       <td>Procedural Interface</td>
       <td>Yes</td>
       <td>No</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>API supports non-blocking, asynchronous queries with mysqlnd</td>
       <td>Yes</td>
       <td>No</td>
       <td>No</td>
      </tr>

      <tr>
       <td>Persistent Connections</td>
       <td>Yes</td>
       <td>Yes</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>API supports Charsets</td>
       <td>Yes</td>
       <td>Yes</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>API supports server-side Prepared Statements</td>
       <td>Yes</td>
       <td>Yes</td>
       <td>No</td>
      </tr>

      <tr>
       <td>API supports client-side Prepared Statements</td>
       <td>No</td>
       <td>Yes</td>
       <td>No</td>
      </tr>

      <tr>
       <td>API supports Stored Procedures</td>
       <td>Yes</td>
       <td>Yes</td>
       <td>No</td>
      </tr>

      <tr>
       <td>API supports Multiple Statements</td>
       <td>Yes</td>
       <td>Most</td>
       <td>No</td>
      </tr>

      <tr>
       <td>API supports Transactions</td>
       <td>Yes</td>
       <td>Yes</td>
       <td>No</td>
      </tr>

      <tr>
       <td>Transactions can be controlled with SQL</td>
       <td>Yes</td>
       <td>Yes</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>Supports all MySQL 5.1+ functionality</td>
       <td>Yes</td>
       <td>Most</td>
       <td>No</td>
      </tr>

     </tbody>
    
   </table>

  </div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqlinfo.terminology.html">Terminology overview</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqlinfo.library.choosing.html">Choosing a library</a></div>
 <div class="up"><a href="mysql.html">Overview of the MySQL PHP drivers</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
